import { parseProjectId } from './localstorage';

const TOKEN_KEY = `${parseProjectId()}-access_token`;
const USERNAME_KEY = 'username';

const isLogin = () => {
	return !!localStorage.getItem(TOKEN_KEY);
};

const getToken = () => {
	return localStorage.getItem(TOKEN_KEY);
};

const setToken = (token: string) => {
	localStorage.setItem(TOKEN_KEY, token);
};

const clearToken = () => {
	localStorage.removeItem(TOKEN_KEY);
};

const getUserName = () => {
	return localStorage.getItem(USERNAME_KEY) || '';
};

const setUserName = (token: string) => {
	localStorage.setItem(USERNAME_KEY, token);
};

const clearUserName = () => {
	localStorage.removeItem(USERNAME_KEY);
};

// const getUserInfo = (): UserInfo => {
//   try {
//     const saveData = localStorage.getItem(USER_INFO_KEY) || '{"nickname": "", "userId": ""}'
//     return JSON.parse(saveData)
//   } catch (error) {
//     return { nickname: '', userId: '' }
//   }
// }

// const setUserInfo = (userInfo: UserInfo): void => {
//   try {
//     const saveJson = JSON.stringify(userInfo)
//     localStorage.setItem(USER_INFO_KEY, saveJson)
//   } catch (error) {
//     // console.error(error)
//   }
// }

// const clearUserInfo = (): void => {
//   localStorage.removeItem(USER_INFO_KEY)
// }

export { isLogin, getToken, setToken, clearToken, getUserName, setUserName, clearUserName };
